package com.sooncode.project.focusservice._.base;

import com.sooncode.project.springcloudcommon.utils.DateUtils;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

public class Utils {
    public static Double doubleFormat2(Double d) {
        Double df=Double.valueOf(String.format("%.2f",d));
        if(d>df) df+=0.01;
        return Double.parseDouble(String.format("%.2f",df));
    }
    public static Double doubleFormat1(Double d) {
        Double df=Double.valueOf(String.format("%.1f",d));
        if(d>df) df+=0.1;
        return Double.parseDouble(String.format("%.1f",df));
    }
    public static Map<String,Object> mapValueTrim(Map<String,Object> map){
        for(Map.Entry<String,Object> entry:map.entrySet()){
            if(entry.getValue() instanceof String){
                entry.setValue(((String)entry.getValue()).trim());
            }
        }
        return map;
    }
    public static String getPatientAge(Date birthDate,Integer[] ageNum,Integer[] monthNum) {
        // 当前日历
        Calendar nowCalendar = Calendar.getInstance();
        // 生日大于当前日期
        if (nowCalendar.before(birthDate)) {
            throw new IllegalArgumentException("The birth date is before current time, it's unbelievable");
        }
        // 当前年月日
        int yearNow = nowCalendar.get(Calendar.YEAR);
        int monthNow = nowCalendar.get(Calendar.MONTH);
        int dayNow = nowCalendar.get(Calendar.DAY_OF_MONTH);
        // 出生日期年月日
        Calendar birthCalendar = Calendar.getInstance();
        birthCalendar.setTime(birthDate);
        int yearBirth = birthCalendar.get(Calendar.YEAR);
        int monthBirth = birthCalendar.get(Calendar.MONTH);
        int dayBirth = birthCalendar.get(Calendar.DAY_OF_MONTH);
        // 粗计算年龄
        int age = yearNow - yearBirth;
        // 当前月份小于出生月份年龄减一
        if (monthNow < monthBirth) { age--; }
        // 当前月份等于出生月份，日小于生日年龄减一
        else if (monthNow == monthBirth && dayNow < dayBirth) { age--; }
        // 粗计月
        int month;
        if (monthNow < monthBirth) {
            month = 12 + monthNow - monthBirth;
        } else {
            month = monthNow - monthBirth;
        }
        int day;
        if (dayNow < dayBirth) {
            day = 30 + dayNow - dayBirth;
            month = month - 1;
        } else {
            day = dayNow - dayBirth;
        }
        if(month<0){
            month=12+month;
        }
        monthNum[0]=month;
        ageNum[0]=age;
        if (age >= 12) {
            return age + "岁";
        } else if (age >= 1) {
            return age + "岁" + (month==0?"":month+"个月");
        } else if (month >= 6) {
            return month + "个月";
        } else if (month >= 1) {
            return month + "个月 " + (day==0?"":day+"天");
        }
        return day + "天";
    }
}
